﻿using System;
using System.ComponentModel.Composition;
using System.Windows.Input;
using IntoNails.DataAccess;
using IntoNails.Infrastructure;
using IntoNails.Infrastructure.Views;
using IntoNails.Models;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;

namespace IntoNails.Modules.CategoryManagement.Controllers
{
    [Export(typeof(ICategoryManagementController))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class CategoryManagementController : ICategoryManagementController
    {
        //[Import]
        private IRegionManager _regionManager;
        private ICategoryRepository _categoryRepository;

        private readonly GlobalCommandProxy _globalCommandProxy;

        [ImportingConstructor]
        public CategoryManagementController(IRegionManager regionManager, ICategoryRepository categoryRepository, GlobalCommandProxy commandProxy)
        {
            if (commandProxy == null)
            {
                throw new ArgumentNullException("commandProxy");
            }

            this._regionManager = regionManager;
            this._categoryRepository = categoryRepository;
            this._globalCommandProxy = commandProxy;

            this.ShowWorkspaceCommand = new DelegateCommand(this.OnShowWorkspaceCommandExecuted);

            #region Category

            this.CreateNewCategoryCommand = new DelegateCommand<Category>(this.OnCreateNewCategoryExecuted);
            this.RenameCategoryCommand = new DelegateCommand<Category>(this.OnRenameCategoryExecuted);
            this.DeleteCategoryCommand = new DelegateCommand<Category>(this.OnDeleteCategoryExecuted);

            #endregion
        }

        public ICommand ShowWorkspaceCommand { get; private set; }

        public ICommand CreateNewCategoryCommand { get; private set; }
        public ICommand RenameCategoryCommand { get; private set; }
        public ICommand DeleteCategoryCommand { get; private set; }

        #region Command Action Handlers

        private void OnShowWorkspaceCommandExecuted()
        {
            Uri workspaceUri = new Uri(@"View/CategoryWorkspaceView", UriKind.Relative);
            this._regionManager.RequestNavigate(RegionNames.WorkspaceRegion, workspaceUri);
        }

        #region Category

        private void OnCreateNewCategoryExecuted(Category parentCategory)
        {
            this.ShowCreateNewCategoryDialog();
        }

        private void OnRenameCategoryExecuted(Category currentCategory)
        {

        }

        private void OnDeleteCategoryExecuted(Category currentCategory)
        {

        }

        #endregion

        #endregion

        #region Private Helpers

        private void ShowCreateNewCategoryDialog()
        {
            ////this._regionManager.Regions.re

            this._regionManager.AddToRegion(RegionNames.SecondaryRegion, new SingleActiveRegion());
        }

        #endregion
    }
}
